package com.gec.hrm.dao.impl;

import com.gec.hrm.pojo.Dept;
import com.gec.hrm.pojo.Employee;
import com.gec.hrm.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class EmployeeDaoImpl extends JDBCUtils<Employee> implements EmployeeDao{
    @Override
    public int addEmployee(Employee employee) {
        return update("insert into employee_inf values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                employee.getName(),employee.getCardId(),employee.getAddress(),
                employee.getPostCode(),employee.getTel(),employee.getPhone(),
                employee.getQqNum(),employee.getEmail(),employee.getSex(),
                employee.getParty(),employee.getBirthday(),employee.getRace(),
                employee.getEducation(),employee.getSpeciality(),employee.getHobby(),
                employee.getRemark(),employee.getCreateDate(),employee.getState(),
                employee.getDeptId(),employee.getJob_id());
    }

    @Override
    public int updEmployee(Employee employee) {
        return update("update employee_inf set name=?,card_id=?,address=?," +
                        "post_code=?,tel=?,phone=?,qq_num=?,email=?,sex=?,party=?," +
                        "birthday=?,race=?,education=?,speciality=?,hobby=?," +
                        "remark=?,dept_id=?,job_id=? where id=?",
                employee.getName(),employee.getCardId(),employee.getAddress(),
                employee.getPostCode(),employee.getTel(),employee.getPhone(),
                employee.getQqNum(),employee.getEmail(),employee.getSex(),
                employee.getParty(),employee.getBirthday(),employee.getRace(),
                employee.getEducation(),employee.getSpeciality(),employee.getHobby(),
                employee.getRemark(),employee.getDeptId(),employee.getJob_id(),
                employee.getId()
                );
    }

    @Override
    public int delEmployee(int id) {
        return update("delete from employee_inf where id=?",id);
    }

    @Override
    public int count() {
        List<Employee> employees= query("select * from employee_inf e,dept_inf d,job_inf j where d.ID=e.dept_id and e.job_id=j.ID");
        return employees.size();
    }

    @Override
    public boolean selectCardId(String cardId) {
        List<Employee> employees = query("select e.*,d.`NAME` deptName,j.`NAME` jobName from employee_inf e,dept_inf d,job_inf j"+
                " where d.ID=e.dept_id and e.job_id=j.ID and card_id=?",cardId);
        if(employees.size()>0){
            return true;
        }
        return false;
    }


    public List<Employee> employeeList(Employee employee,int page,int limit)
    {
        String sql="select e.*,d.`NAME` deptName,j.`NAME` jobName from employee_inf e,dept_inf d,job_inf j where d.ID=e.dept_id and e.job_id=j.ID";
        if(employee.getName()!=null && !"".equals(employee.getName())){
            sql+= " and e.name like '%"+employee.getName()+"%'";
        }
        if(employee.getJob_id()!=0){
            sql+= " and job_id = "+employee.getJob_id()+"";
        }
        if(employee.getDeptId()!=0){
            sql+= " and dept_id = "+employee.getDeptId()+"";
        }
        if(employee.getSex()!=-1){
            sql+= " and sex = "+employee.getSex()+"";
        }
        if(employee.getCardId()!=null && !"".equals(employee.getCardId())){
            sql+= " and card_id = "+employee.getCardId()+"";
        }
        if(employee.getPhone()!=null && !"".equals(employee.getPhone())){
            sql+= " and phone = "+employee.getPhone()+"";
        }
        sql+=" limit ?,?";
        List<Employee> employees = query(sql,(page-1)*limit,limit);
        return employees;
    }


    public Employee getBean(ResultSet rs){
        Employee employee = new Employee();
        try {
            employee.setId(rs.getInt("id"));
            employee.setName(rs.getString("name"));
            employee.setCardId(rs.getString("card_id"));
            employee.setAddress(rs.getString("address"));
            employee.setPostCode(rs.getString("post_code"));
            employee.setTel(rs.getString("tel"));
            employee.setPhone(rs.getString("phone"));
            employee.setQqNum(rs.getString("qq_num"));
            employee.setEmail(rs.getString("email"));
            employee.setSex(rs.getInt("sex"));
            employee.setParty(rs.getString("party"));
            employee.setBirthday(rs.getDate("birthday"));
            employee.setRace(rs.getString("race"));
            employee.setEducation(rs.getString("education"));
            employee.setSpeciality(rs.getString("speciality"));
            employee.setHobby(rs.getString("hobby"));
            employee.setRemark(rs.getString("remark"));
            employee.setCreateDate(rs.getDate("CREATE_DATE"));
            employee.setDeptId(rs.getInt("dept_id"));
            employee.setJob_id(rs.getInt("job_id"));
            employee.setDeptName(rs.getString("deptName"));
            employee.setJobName(rs.getString("jobName"));
            employee.setState(rs.getInt("state"));
            return employee;
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return null;
    }

}
